package com.fenghuajueli.libbasecoreui.manager;

import com.fenghuajueli.lib_data.entity.db.DayPlanDbEntity;
import com.fenghuajueli.lib_data.entity.db.MemoDbEntity;
import com.fenghuajueli.lib_data.entity.db.MemoDbEntityDao;
import com.fenghuajueli.libbasecoreui.BaseApplication;

import java.util.List;

public class MemoManager {

    private static class MemoManagerSingleton {
        private static final MemoManager INSTANCE = new MemoManager();

    }

    public static MemoManager getInstance() {
        return MemoManager.MemoManagerSingleton.INSTANCE;
    }

    private MemoManager() {
    }

    public void saveMemoDbEntity(MemoDbEntity memoDbEntity) {
        List<MemoDbEntity> list = BaseApplication.getMemoDaoMaster().newSession().getMemoDbEntityDao().queryBuilder()
                .where(MemoDbEntityDao.Properties.Create_time.eq(memoDbEntity.getCreate_time())).list();
        if (list != null && list.size() > 0) {
            BaseApplication.getMemoDaoMaster().newSession().getMemoDbEntityDao().updateInTx(memoDbEntity);
        } else {
            BaseApplication.getMemoDaoMaster().newSession().getMemoDbEntityDao().insertOrReplace(memoDbEntity);
        }
    }

    public List<MemoDbEntity> getMemoData(int ptType) {
        List<MemoDbEntity> list = BaseApplication.getMemoDaoMaster().newSession().getMemoDbEntityDao().queryBuilder()
                .where(MemoDbEntityDao.Properties.Type.eq(ptType)).list();
        return list;
    }

    public List<MemoDbEntity> deleteMemoDbEntity(MemoDbEntity memoDbEntity) {
        List<MemoDbEntity> list = BaseApplication.getMemoDaoMaster().newSession().getMemoDbEntityDao()
                .queryBuilder().where(MemoDbEntityDao.Properties.Create_time.eq(memoDbEntity.getCreate_time())).list();
        if (list != null && list.size() > 0) {
            BaseApplication.getMemoDaoMaster().newSession().getMemoDbEntityDao().deleteInTx(list);
        }
        return getMemoData(memoDbEntity.getType());
    }

    public List<MemoDbEntity> updateMemoDbEntity(MemoDbEntity memoDbEntity) {
        List<MemoDbEntity> memoData = getMemoData(memoDbEntity.getType());
        for (MemoDbEntity memoDatum : memoData) {
            memoDatum.setIsTop(false);
            if (memoDatum.getCreate_time() == memoDbEntity.getCreate_time()) {
                memoDatum.setIsTop(true);
            }
        }
        BaseApplication.getMemoDaoMaster().newSession().getMemoDbEntityDao().updateInTx(memoData);
        return getMemoData(memoDbEntity.getType());
    }

}
